import { defineStore } from 'pinia';
import type * as Type from './type';
import * as DataFunc from '@/utils/fileReadAdapter';
import { ElMessage } from "element-plus";
// import router from "@/router";

const useFilmRead = defineStore("filmread", {
  state: (): Type.FilmRead => ({
    currentSeriesIndex: 0,
    currentThumbnailIndex: 0,
    interfaceData: {} as Type.InterfaceData,
    dictionaryData: {} as Type.DictionaryData,
    loading: false
  }),
  getters: {},
  actions: {
    // 派发state
    setState(partial: Partial<Type.FilmRead>) {
      this.$patch(partial);
    },
    async getSettingData(route: FilmRead.RouteParams) {
      // console.log(router.get);
      const params = {
        ...route,
        id: route.id as unknown as string
      }
      if (route.dataType === 'multiple') {
        // 多序列展示上下按钮
        this.interfaceData = Object.assign(this.interfaceData, await DataFunc.getTaskInfoInterface(params));
        // 点击所有已提交序列的按钮后的逻辑
        sessionStorage.setItem("task_info", JSON.stringify(this.interfaceData.seriesAllList[this.currentSeriesIndex]));
      }
      // 不展示按钮
      // await DataFunc.getInterface(params, this.$state);
      this.interfaceData = Object.assign( this.interfaceData, await DataFunc.getInterface(params));
      if(this.interfaceData.seriesList === null){
        ElMessage.error('获取不到新切片，请返回')
        return
      }
      await this.getResultData();
    },
    async getResultData() {
      // await DataFunc.getResultInterface(this.interfaceData.seriesList[this.currentSeriesIndex].id)
      this.interfaceData = Object.assign(this.interfaceData, await DataFunc.getResultInterface(this.interfaceData.seriesList[this.currentThumbnailIndex].id))
      console.log(this.interfaceData, 'this.interfaceData');
      this.getDictionaryData();
    },
    getDictionaryData() {
      this.dictionaryData = Object.assign(this.dictionaryData, DataFunc.getDictionaryData(this.interfaceData), DataFunc.getResultDictData(this.interfaceData, DataFunc.getDictionaryData(this.interfaceData)));
      console.log(this.dictionaryData, '处理');
    },
    updateThumbnailIndex(index: number) {
      this.currentThumbnailIndex = index;
      this.getResultData();
    },
    prevIndex(route: FilmRead.RouteParams) {
      this.currentSeriesIndex--;
      this.currentSeriesIndex < 0 && (this.currentSeriesIndex = 0);
      sessionStorage.setItem("task_info", JSON.stringify(this.interfaceData.seriesAllList[this.currentSeriesIndex]));
      // const route = useRoute();
      // const params = {
      //   ...route.query,
      //   id: route.query.id as unknown as string
      // }
      // this.interfaceData = Object.assign({}, this.interfaceData, await DataFunc.getInterface(params));
      // await this.getResultData();
      this.loading = true;
      // this.getSettingData(route);
    },
    nextIndex(route: FilmRead.RouteParams) {
      this.currentSeriesIndex++;
      this.currentSeriesIndex > this.interfaceData.seriesAllList.length - 1 && (this.currentSeriesIndex = this.interfaceData.seriesAllList.length);
      // console.log(this.currentSeriesIndex, "currentSeriesIndex");
      sessionStorage.setItem("task_info", JSON.stringify(this.interfaceData.seriesAllList[this.currentSeriesIndex]));
      // const route = useRoute();
      // const params = {
      //   ...route.query,
      //   id: route.query.id as unknown as string
      // }
      // this.interfaceData = Object.assign({}, this.interfaceData, await DataFunc.getInterface(params));
      // await this.getResultData();
      this.loading = true;
      // this.getSettingData(route);
    },
    setLesionList(list: {[x: string | number]: Type.LesionItem | Type.LesionItem[] }) {
      this.dictionaryData.lesionDataList && (this.dictionaryData.lesionDataList = list)
    }
  }
})

export default useFilmRead;